 package handling.world;


import client.ClientRedirector;
import client.MapleClient;
import configs.ServerConfig;
import handling.Auction.AuctionServer;
import handling.cashshop.CashShopServer;
import handling.channel.ChannelServer;
import handling.channel.PlayerStorage;
import server.shops.HiredFisher;
import server.shops.HiredMerchant;
import tools.CollectionUtil;

import java.util.*;















 public class World {
   public static void init() {
/*  19 */     WorldFindService.getInstance();
/*  20 */     WorldBroadcastService.getInstance();
/*  21 */     WorldPartyService.getInstance();
/*  22 */     WorldAllianceService.getInstance();
/*  23 */     WorldBuddyService.getInstance();
/*  24 */     WorldFamilyService.getInstance();
/*  25 */     WorldGuildService.getInstance();
/*  26 */     WorldMessengerService.getInstance();
   }

   public static String getStatus() {
/*  30 */     StringBuilder ret = new StringBuilder();
/*  31 */     int totalUsers = 0;
/*  32 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/*  33 */       ret.append("频道 ");
/*  34 */       ret.append(cs.getChannel());
/*  35 */       ret.append(": ");
/*  36 */       int channelUsers = cs.getConnectedClients();
/*  37 */       totalUsers += channelUsers;
/*  38 */       ret.append(channelUsers);
/*  39 */       ret.append(" 玩家\n");
     }
/*  41 */     ret.append("总计在线: ");
/*  42 */     ret.append(totalUsers);
/*  43 */     ret.append("\n");
/*  44 */     return ret.toString();
   }

   public static Map<Integer, Integer> getConnected() {
/*  48 */     Map<Integer, Integer> ret = new LinkedHashMap<>();
/*  49 */     int total = 0;
/*  50 */     for (ChannelServer ch : ChannelServer.getAllInstances()) {
/*  51 */       int chOnline = ch.getConnectedClients();
/*  52 */       ret.put(Integer.valueOf(ch.getChannel()), Integer.valueOf(chOnline));
/*  53 */       total += chOnline;
     }
/*  55 */     int csOnline = CashShopServer.getConnectedClients();
/*  56 */     int mtsOnline = AuctionServer.getConnectedClients();
/*  57 */     ret.put(Integer.valueOf(-10), Integer.valueOf(csOnline));
/*  58 */     ret.put(Integer.valueOf(-20), Integer.valueOf(mtsOnline));
/*  59 */     total += csOnline;

/*  61 */     ret.put(Integer.valueOf(0), Integer.valueOf(total));
/*  62 */     return ret;
   }

   public static List<CheaterData> getCheaters() {
/*  66 */     List<CheaterData> allCheaters = new ArrayList<>();
/*  67 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/*  68 */       allCheaters.addAll(cs.getCheaters());
     }
/*  70 */     Collections.sort(allCheaters);
/*  71 */     return CollectionUtil.copyFirst(allCheaters, 20);
   }

   public static List<CheaterData> getReports() {
/*  75 */     List<CheaterData> allCheaters = new ArrayList<>();
/*  76 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/*  77 */       allCheaters.addAll(cs.getReports());
     }
/*  79 */     Collections.sort(allCheaters);
/*  80 */     return CollectionUtil.copyFirst(allCheaters, 20);
   }

   public static boolean isConnected(String charName) {
/*  84 */     return (WorldFindService.getInstance().findChannel(charName) > 0);
   }

   public static void toggleMegaphoneMuteState() {
/*  88 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/*  89 */       cs.toggleMegaphoneMuteState();
     }
   }

   public static void ChannelChange_Data(CharacterTransfer Data, int characterid, int toChannel) {
/*  94 */     getStorage(toChannel).registerPendingPlayer(Data, characterid);
   }

   public static boolean isCharacterListConnected(List<String> charNames) {
/*  98 */     for (ChannelServer cserv : ChannelServer.getAllInstances()) {
/*  99 */       for (String name : charNames) {
/* 100 */         if (cserv.isConnected(name)) {
/* 101 */           return true;
         }
       }
     }
/* 105 */     return false;
   }

   public static String getAllowLoginTip(List<String> charNames) {
/* 109 */     StringBuilder ret = new StringBuilder("账号下其他角色在游戏: ");
/* 110 */     for (ChannelServer cserv : ChannelServer.getAllInstances()) {
/* 111 */       for (String name : charNames) {
/* 112 */         if (cserv.isConnected(name)) {
/* 113 */           ret.append(name);
/* 114 */           ret.append(" ");
         }
       }
     }
/* 118 */     return ret.toString();
   }




   public static boolean hasMerchant(int accountID) {
/* 125 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/* 126 */       if (cs.containsMerchant(accountID)) {
/* 127 */         return true;
       }
     }
/* 130 */     return false;
   }




   public static boolean hasMerchant(int accountID, int characterID) {
/* 137 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/* 138 */       if (cs.containsMerchant(accountID, characterID)) {
/* 139 */         return true;
       }
     }
/* 142 */     return false;
   }





   public static HiredMerchant getMerchant(int accountID, int characterID) {
/* 150 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/* 151 */       if (cs.containsMerchant(accountID, characterID)) {
/* 152 */         return cs.getHiredMerchants(accountID, characterID);
       }
     }
/* 155 */     return null;
   }

   public static PlayerStorage getStorage(int channel) {
/* 159 */     if (channel == -20)
/* 160 */       return AuctionServer.getPlayerStorage(); 
/* 161 */     if (channel == -10) {
/* 162 */       return CashShopServer.getPlayerStorage();
     }
/* 164 */     return ChannelServer.getInstance(channel).getPlayerStorage();
   }

   public static boolean isChannelAvailable(int ch) {
/* 168 */     return (ChannelServer.getInstance(ch) != null && ChannelServer.getInstance(ch).getPlayerStorage() != null && ChannelServer.getInstance(ch).getPlayerStorage().getConnectedClients() < ServerConfig.LOGIN_USERLIMIT);
   }

   public static boolean hasFisher(int accountID, int characterID) {
/* 172 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/* 173 */       if (cs.containsFisher(accountID, characterID)) {
/* 174 */         return true;
       }
     }
/* 177 */     return false;
   }

   public static HiredFisher getFisher(int accountID, int characterID) {
/* 181 */     for (ChannelServer cs : ChannelServer.getAllInstances()) {
/* 182 */       if (cs.containsFisher(accountID, characterID)) {
/* 183 */         return cs.getHiredFisher(accountID, characterID);
       }
     }
/* 186 */     return null;
   }

   public static class Redirector
   {
/* 191 */     private static final Map<String, ClientRedirector> redirectors = new HashMap<>();

     public static String addRedirector(MapleClient c) {
/* 194 */       for (Map.Entry<String, ClientRedirector> redirector : redirectors.entrySet()) {
/* 195 */         if (((ClientRedirector)redirector.getValue()).getAccount().equals(c.getAccountName())) {
/* 196 */           redirectors.remove(redirector.getKey());
           break;
         }
       }
/* 200 */       Random random = new Random();
/* 201 */       String code = "";
/* 202 */       for (int i = 0; i < 200; i++) {
/* 203 */         int temp; switch (random.nextInt(10)) {
           case 0:
/* 205 */             code = code + "_";
             break;
           case 1:
/* 208 */             code = code + "~";
             break;
           case 2:
           case 3:
           case 4:
           case 5:
/* 214 */             temp = (random.nextInt(2) == 0) ? 65 : 97;
/* 215 */             code = code + (char)(random.nextInt(26) + temp);
             break;
           default:
/* 218 */             code = code + String.valueOf(random.nextInt(10)); break;
         }
       }
/* 221 */       redirectors.put(code, new ClientRedirector(c.getAccountName(), c.getWorld(), c.getChannel(), false));
/* 222 */       return code;
     }

     public static Map<String, ClientRedirector> getRedirectors() {
/* 226 */       return redirectors;
     }
   }

   public static class Client
   {
/* 232 */     private static final ArrayList<MapleClient> clients = new ArrayList<>();

     public static void addClient(MapleClient c) {
/* 235 */       if (!clients.contains(c)) {
/* 236 */         clients.add(c);
       }
     }

     public static boolean removeClient(MapleClient c) {
/* 241 */       return clients.remove(c);
     }

     public static ArrayList<MapleClient> getClients() {
/* 245 */       return clients;
     }
   }
 }